$(document).ready(doInit);

function doInit() {
	queryApparatusList({});
	
	$('#search').click(doSearch);
}

function doSearch() {
	queryApparatusList({
		name : $.trim($('#name').val())	
	});
}

function queryApparatusList(queryParams) {
	$('#apparatus').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    url:'apparatus-admin!getApparatusVOList.action',
	    idField: 'appCode',
	    queryParams :queryParams,
	    rownumbers : true,
	    singleSelect:true,
	    columns:[[
	        {field:'name',title:'仪器名称',width:fixWidth(0.15),sortable:true},
	        {field:'appGroupName',title:'种类',width:fixWidth(0.13),sortable:true},
	        {field:'devName',title:'设备名称',width:fixWidth(0.08)},
	        {field:'appAdminName', title:'仪器管理员',width:fixWidth(0.08)},
	        {field:'setupCharges', title:'开机费(元)',width:fixWidth(0.06),sortable:true},
	        {field:'machHourFee', title:'机时费(元)',width:fixWidth(0.06),sortable:true},
	        {field:'overTimeCharge', title:'超时罚金(元)',width:fixWidth(0.08),sortable:true},
	        {field:'noUseCharge', title:'未使用罚金(元)',width:fixWidth(0.08),sortable:true},
	        {field:'otherFee', title:'其它费用(元)',width:fixWidth(0.07),sortable:true}, 
	        {field:'isNeedOrder', title:'是否需要预约', width:fixWidth(0.08),
	        		formatter : function(value, row, index) {
						if (value == '1') {
							return '是';
				 		} else {
							return '否';
						}
					}
	        },
	        
	        {field:'appCode2',title:'操作',width:fixWidth(0.18),align:'center',
	        	    formatter : function(value, row, index) {
	        	    	var e = '<a href="#" style="color:red;text-decoration:none;" onclick="editrow('+ index + ',\'' + row.appCode +'\')">编辑</a> ';
	        	    	var f = '<a href="#" style="color:red;text-decoration:none;" onclick="showDetail('+ index + ',\'' + row.appCode +'\')">仪器详情</a> ';
	        	    	var g = '<a href="#" style="color:red;text-decoration:none;" onclick="getApparatusConfigs('+ index + ',\'' + row.appCode +'\')">配件详情</a> ';
						return e + f + g;
					}
			}
	    ]],
	    pagination:true,
	    fitColumns : true
	});
}


function editrow(index,value){
	if ('' == $.trim(value)) {
		$.messager.alert('确定', '<b>请刷新页面后再尝试</b>', 'warning');
		return false;
	}
	
	var url = 'apparatus!getTheApparatusVo.action?appCode=' + value;
	
	var title='编辑仪器信息';
	if (parent.$('#content').tabs('exists', title)){
        parent.$('#content').tabs('select', title);
    } else {
        var content = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
        parent.$('#content').tabs('add',{
            title:title,
            content:content,
            closable:true
        });
    }
}


function showDetail(index, value) {
	var title = '仪器详情';
	var url   = "apparatus!showApparatus.action?appCode=" + value + "&position=back&time=" + new Date();
	var content = '<iframe scrolling="auto" frameborder="0"  src="'+ url +'" style="width:100%;height:100%;"></iframe>';
	
	if (parent.$('#content').tabs('exists', title)) {
		parent.$('#content').tabs('select', title);
		parent.$('#content').tabs('update', {
			tab : parent.$('#conent').tabs('getTab', title),
			options: {
				content : content
			}
		});
	} else {
		parent.$('#content').tabs('add', {
			title : title,
			content : content,
			closable : true
		});
	}
}


function getApparatusConfigs(index, value) {
	if ('' == $.trim(value)) {
		$.messager.alert('确定', '<b>请刷新页面后再尝试</b>', 'warning');
		return false;
	}
	
	var url = 'apparatus-config!queryApparatusConfigs.action?appCode=' + value + '&time='+ new Date();
	
	var title='仪器配件信息';
	if (parent.$('#content').tabs('exists', title)){
        var tab = parent.$('#content').tabs('getTab', title);
				      	  
	    var content = '<iframe scrolling="auto" frameborder="0"  src="'+ url +'" style="width:100%;height:100%;"></iframe>';
        
        parent.$('#content').tabs('update', {
				tab: tab,
				options:{
					content:content
				}
			});
        
       parent.$('#content').tabs('select', title);
        
    } else {
        var content = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
        parent.$('#content').tabs('add',{
            title:title,
            content:content,
            closable:true
        });
    }
}